<?php

    class PlayerModel extends AbstractModel {

        public static function getByAccount(AccountsModel $Account) {
            $Repository = new SQLRepository('Players');

            $Repository->addColumn('level');
            $Repository->addColumn('name');
            $Repository->addColumn('online');

            $criteria = new SQLCriteria();
            $criteria->add(new SQLFilter('account_id', '=', $Account->getId()));
            $criteria->setProperty('order', 'level DESC');

            return $Repository->load($criteria);
        }

        public static function getByName(PlayersModel $Player) {
            $Select = new SQLSelect();
            $Select->setEntity('Players', 'p');

            $Select->addColumns(array(
                'p.account_id',
                'p.name',
                'p.sex',
                'p.vocation',
                'p.promotion',
                'p.level',
                'p.lastlogin',
                'p.online',
                'a.premdays'
            ));

            $Join = new SQLJoin();
            $Join->setEntity('Accounts', 'a');
            $criteria = new SQLCriteria();
            $criteria->add(new SQLFilter('a.id', '=', 'p.account_id', true));
            $Join->setCriteria($criteria);

            $Select->addJoin($Join);

            $criteria = new SQLCriteria();
            $criteria->add(new SQLFilter('p.name', '=', $Player->getName()));
            $criteria->setProperty('LIMIT', '0, 1');

            $Select->setCriteria($criteria);

            $rows = $Select->execute();
            if ($rows->rowCount() == 1) {
                $row = $rows->fetch(PDO::FETCH_ASSOC);
                $Player->fromArray($row);
                return true;
            }
            return false;
        }


}
?>
